package com.tutor.lzm;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.tutor.lzm.activity.LoginActivity;
import com.tutor.lzm.activity.MainActivity;
import com.tutor.lzm.app.Constant;
import com.tutor.syslib.Logs;
import com.tutor.syslib.action.PermissionManager;
import com.tutor.syslib.util.MessageTools;

/**
 * Created by zzgpc on 2017-8-24.
 */

public class SplashActivity extends Activity {
    private ImageView imageView;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            finish();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_splash);
        if (getIntent() != null && getIntent().hasExtra(Constant.isExit)) {
            if (getIntent().getBooleanExtra(Constant.isExit, false)) {
                finish();
                return;
            }
        }
        imageView = (ImageView) findViewById(R.id.imageView);
        Picasso.with(this)
                .load(R.mipmap.splash)
                .into(imageView);
        if (Build.VERSION.SDK_INT >= 23) {
            PermissionManager.requestIEMPermission(this);
            PermissionManager.requestSDPermission(this);
            PermissionManager.requestCameraPermission(this);
        }
        handler.sendEmptyMessageDelayed(1, 3 * 1000);


    }

    /**
     * 用户进行权限设置后的回调函数 , 来响应用户的操作，无论用户是否同意权限，Activity都会
     * 执行此回调方法，所以我们可以把具体操作写在这里
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == PermissionManager.REQUEST_CODE_ASK_LOCATION_PERMISSIONS) {
            if ((grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED)) {
//                PushManager.getInstance().initialize(this.getApplicationContext(), userPushService);
            } else {
                Logs.e("SplashAct", "We highly recommend that you need to grant the special permissions before initializing the SDK, otherwise some "
                        + "functions will not work");
//                MessageTools.showToast(getApplicationContext(), "您未开启定位权限，禁止该权限会导致app某些功能异常");
//                PushManager.getInstance().initialize(this.getApplicationContext(), userPushService);
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
